<?php
namespace app\index\controller;

use app\index\controller\BaseController;
use app\model\Article;
use app\model\Message;
use think\facade\Db;
use think\facade\Cache;
use think\facade\View;



class Index extends BaseController
{

    public function index()
    {
        //这里请使用缓存
        if(request()->isMobile() and $this->system['is_mob']=='启用'){
            return View::fetch('m_'.'index');
        }else{
            return View::fetch();
        }
    }
    public function ajax()
    {
        $data=input('post.');
        $lizhili=$this->jisuan($data,$time=input('time',0));
        if ($lizhili['code']==400) {
            return json(['code'=>0,'message'=>$lizhili['msg']]);
        }
        if($data['type']=='get_my_info'){
            return  json(['code'=>200,'message'=>'成功！']);
        }

        return json(['code'=>400,'message'=>'非法获取！']);
    }
    public function cate()
    {
        //拦截远程跳转
        $cate=Db::name('cate')->find(\input('id'));
        if($cate['type']==4 and isset($cate['url'])){
            redirect($cate['url'],302);
        }
        $html=$cate['catehtml'];
        View::assign('id', input('id'));
        if(substr($html,-5,5)=='.html'){
            $html=substr($html,0,strlen($html)-5);
        }

        if($html){
            if(request()->isMobile() and $this->system['is_mob']=='启用'){
                return View::fetch('m_'.$html);
            }else{
                return View::fetch($html);
            }
        }else{
            if(request()->isMobile() and $this->system['is_mob']=='启用'){
                return View::fetch('m_cate');
            }else{
                return View::fetch();
            }
        }
    }
    public function search()
    {
        $article = new Article();
        $keyword=input('key','');
        $cateid=input('cateid',0);
        $num=input('num') ?? 10;
        $catename='全部';
        $where=true;
        if($cateid){
            $catename=Db::name('cate')->where('id',$cateid)->value('catename');
            $where=[
                'cateid'=>$cateid
            ];
        }
        $list=$article->where('isopen',1)->where($where)->where('title','like','%'.$keyword.'%')->paginate($num,false,['query'=>request()->param()]);
        $page = $list->render();
        // 模板变量赋值
        View::assign('list', $list);
        View::assign('page', $page);
        if(request()->isMobile() and $this->system['is_mob']=='启用'){
            return View::fetch('m_search',[
                'key'=>$keyword,
                'catename'=>$catename,
            ]);
        }else{
            return View::fetch('',[
                'key'=>$keyword,
                'catename'=>$catename,
            ]);
        }
    }
    public function shuoming()
    {
        $id=input('id',0);
        $article=Db::name('shuoming')->find($id);
        if(!$article){
            header("Location:/404.html");
            die;
        }
        if(isset($article['url']) and $article['url']){
            redirect($article['url'],302);
        }
        Db::name('shuoming')->where('id',$article['id'])->update([
            'click_wai'=>$article['click_wai']+1,
            'click'=>$article['click']+1,
        ]);
        if(request()->isMobile() and $this->system['is_mob']=='启用'){
            return View::fetch('m_shuoming',[
                'data'=>$article
            ]);
        }else{
            return View::fetch('',[
                'data'=>$article
            ]);
        }
    }
    public function show()
    {
        $article = new Article();
        $id=input('id');
        $article=$article->find($id);
        if(!$article){
            header("Location:/404.html");
            die;
        }
        if(isset($article['url']) and $article['url']){
            redirect($article['url'],302);
        }
        $article->where('id',$article['id'])->update([
            'click_wai'=>$article['click_wai']+1,
            'click'=>$article['click']+1,
        ]);

        $shang1=$article->where('id','<',$id)->where('cateid',$article['cateid'])->order('id desc')->find();
        if($shang1){
            $shang="<a href='".url('index/show',['id'=>$shang1['id']])."'>".$shang1['title']."</a>";
            $shangurl=url('index/show',['id'=>$shang1['id']]);
        }else{
            $shang="<a href='".url('index/cate',['id'=>$article['cateid']])."'>返回列表</a>";
            $shangurl=url('index/cate',['id'=>$article['cateid']]);
        }
        $xia1=$article->where('id','>',$id)->where('cateid',$article['cateid'])->order('id asc')->find();
        if($xia1){
            $xia="<a href='".url('index/show',['id'=>$xia1['id']])."'>".$xia1['title']."</a>";
            $xiaurl=url('index/show',['id'=>$xia1['id']]);
        }else{
            $xia="<a href='".url('index/cate',['id'=>$article['cateid']])."'>返回列表</a>";
            $xiaurl=url('index/cate',['id'=>$article['cateid']]);
        }
        View::assign([
            'shang'=>$shang,
            'xia'=>$xia,
            'shangurl'=>$shangurl,
            'xiaurl'=>$xiaurl
        ]);


        $html=Db::name('cate')->where('id',$article['cateid'])->value('showhtml');
        if(substr($html,-5,5)=='.html'){
            $html=substr($html,0,strlen($html)-5);
        }
        if($html){
            if(request()->isMobile() and $this->system['is_mob']=='启用'){
                return View::fetch('m_'.$html);
            }else{
                return View::fetch($html);
            }
        }else{
            if(request()->isMobile() and $this->system['is_mob']=='启用'){
                return View::fetch('m_show');
            }else{
                return View::fetch();
            }
        }
    }
    public function update()
    {
        if(request()->isPost()){
            $data=input('post.');
            $validate = new \app\index\validate\Update;
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            if($data['type']=='message'){
                $message=new Message();
                $res=$message->save([
                    'title'=>$data['title'],
                    'name'=>$data['loginname'],
                    'phone'=>$data['phone'],
                    'neirong'=>$data['need']
                ]);
                if($res){
                    $this->success('留言成功');
                }else{
                    $this->error('留言失败');
                }
            }
        }
        return redirect('/');
    }
}
